import autoit
from selenium import webdriver

import time

from selenium.webdriver.common.by import By

# 初始化Chrome浏览器
driver = webdriver.Chrome()

# 访问测试页面
driver.get("file:///F:/dowload%E4%B8%8B%E8%BD%BD/TestProject/TestProject/upload.html")
driver.maximize_window()

try:
    # 定位文件上传按钮并点击
    upload_button = driver.find_element(By.XPATH, "/html/body/form/input[2]")
    upload_button.click()

    # 使用AutoIt上传文件
    autoit.win_wait("打开", 10)
    autoit.control_send("打开", "Edit1", r"F:\TestProject\ss.txt")  # 替换为你的文件路径
    autoit.control_click("打开", "Button1")

    # 等待几秒观察结果
    time.sleep(3)

    # 截图保存
    driver.save_screenshot("autoit_upload.png")
    print("文件上传成功，截图已保存")

except Exception as e:
    print(f"操作失败：{e}")

finally:
    # 关闭浏览器
    driver.quit()